package org.pearl.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hnmqet.demo03.service.StorageTccService;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.boot.autoconfigure.properties.client.LockProperties;
import io.seata.spring.boot.autoconfigure.properties.client.UndoProperties;
import lombok.extern.slf4j.Slf4j;
import org.pearl.storage.dao.StorageTblMapper;
import org.pearl.storage.entity.StorageTbl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by TD on 2022/1/26
 */
@Slf4j
@Service
public class StorageTccServiceImpl implements StorageTccService {


    @Autowired
    StorageTblMapper storageTblMapper;
    @Autowired
    UndoProperties undoProperties;
    @Autowired
    LockProperties lockProperties;

    @Override
    public void reduceStorage(String code, Long count) {
        log.info("==============================Assign Service Begin ... xid: " + RootContext.getXID() + "\n");
        StorageTbl storageTbl = storageTblMapper.selectOne(new LambdaQueryWrapper<StorageTbl>().eq(StorageTbl::getCommodityCode, code));

        //storageTbl.setId(RandomUtil.randomInt());
        //storageTblMapper.insert(storageTbl);

        String s = undoProperties.toString();
        System.err.println("=================2222222222222222==" + undoProperties.isDataValidation());
        System.err.println("==============23333333333333333333=====" + undoProperties.isOnlyCareUpdateColumns());
        System.err.println("=============" + lockProperties.getRetryTimes());


        System.err.println(storageTbl.toString());
        storageTbl.setCount((int) (storageTbl.getCount() - count));
        //storageTbl.setCreateTime("2020-08-28 16:55:51");
        //storageTbl.setUpdateTime("2020-08-30 16:55:52");
        storageTblMapper.update(storageTbl, new LambdaUpdateWrapper<StorageTbl>().eq(StorageTbl::getCommodityCode, "iphone11"));
        int aaa = 5 / 0;

        // throw  new RuntimeException();

    }


    @Override
    public boolean commit(BusinessActionContext actionContext) {
        String xid = actionContext.getXid();
        return true;
    }

    @Override
    public boolean rollback(BusinessActionContext actionContext) {
        String xid = actionContext.getXid();
        Object count = actionContext.getActionContext("count");
        Object code = actionContext.getActionContext("code");
        // 还原存库
        StorageTbl storageTbl = storageTblMapper.selectOne(new LambdaQueryWrapper<StorageTbl>().eq(StorageTbl::getCommodityCode, code));
        storageTbl.setCount(storageTbl.getCount() +Integer.parseInt(count.toString()));
        storageTbl.setCommodityCode(code.toString());
        storageTblMapper.update(storageTbl, new LambdaUpdateWrapper<>());
        log.info(" rollback, xid:" + xid);
        return true;
    }
}
